/// <reference path="../base.js" />
/// <reference path="../3rdParty/Mootools-Core.js" />

Panels.Pricelist.DisplaySaved = function(result)
{
  var pricelists = result["Pricelists"];
  var select = $("SavedPricelists");
  pricelists.sort();

  for (var i = 0; i < pricelists.length; i++)
  {
    var pricelist = pricelists[i];

    new Element("option", { value: pricelist, html: pricelist })
        .addEvent("click", function() { $("PricelistName").set("value", this.value); })
        .inject(select, "bottom");
  }
}

EventSink.addEvent(MsgLogin, Panels.Pricelist.DisplaySaved);

Panels.Pricelist.New = function()
{
  var name = $("PricelistName").get("value");
  var saved = $("SavedPricelists").getElements("option");

  if (name == "" || $("SavedPricelists").getElements("option").get("value").contains(name))
  {
    alert("Pricelist name must be unique.");
    return;
  }

  var pricelist = Utility.Pricelist.GetBlank(name);

  Utility.Server.AddPricelist(name, Utility.Pricelist.Export(pricelist), (function(result, error)
  {
    if (error)
      return;

    new Element("option", { value: this.pricelist.name, html: this.pricelist.name })
      .addEvent("click", function(){ $("PricelistName").set("value", this.value); })
      .inject($("SavedPricelists"), "bottom");

    Global.Pricelists[Global.CurrentPricelist] = this.pricelist;
    Panels.Pricelist.Render();
  }).bind({ pricelist: pricelist }));
}

Panels.Pricelist.Save = function()
{
  var name = $("PricelistName").get("value");
  var pricelists = $("SavedPricelists").getElements("option");
  var element = $("SavedPricelists").getSelected()[0];

  if (!$chk(element))
    return;

  if (name == element.get("value")) // Same name
  {
    Utility.Server.UpdatePricelist(name, Utility.Pricelist.Export(), $empty); // We don't need to do anything here.
  }
  else
  {
    if (name == "" || pricelists.get("value").contains(name))
    {
      alert("Pricelist name must be unique!");
      return;
    }

    Utility.Server.RenamePricelist(element.get("value"), name, (function(result, error)
    {
      if (error)
        return;

      Global.Pricelists[Global.CurrentPricelist].name = this.name;
      this.element.set("value", this.name).set("html", this.name);

      Utility.Server.UpdatePricelist(this.name, Utility.Pricelist.Export(), $empty); // Nothing to be done here

    }).bind({ name: name, element: element }));
  }
}

Panels.Pricelist.Load = function()
{
  var selected = $("SavedPricelists").getSelected()[0];

  if (!$chk(selected))
    return;

  var name = selected.get("value");

  Utility.Server.GetPricelist(name, (function(code)
  {
    pricelist = Utility.Pricelist.Import(code);
    Global.Pricelists[Global.CurrentPricelist] = pricelist;
    Panels.Pricelist.Render();
  }).bind({ name: name }));
}

Panels.Pricelist.Delete = function()
{
  var selected = $("SavedPricelists").getSelected()[0];

  if (!$chk(selected))
    return;

  if (!confirm("Are you sure you want to delete this pricelist?"))
    return;

  var name = selected.get("value");

  Utility.Server.DeletePricelist(name, (function(result, error)
  {
    if (error)
      return;

    this.selected.destroy();
  }).bind({ selected: selected }));
}

Panels.Pricelist.Import = function()
{
  var code = prompt("Paste the pricelist code here:");

  if (!$chk(code))
    return;

  code = code.trim();

  if (!$chk(code.trim()))
    return;

  try
  {
    var pricelist = Utility.Pricelist.Import(code);
    Global.Pricelists[Global.CurrentPricelist] = pricelist;
    Panels.Pricelist.Render();
  }
  catch (error)
  {
    alert("Error Loading Pricelist");
  }
}

Panels.Pricelist.Export = function()
{
  prompt("Pricelist Code:", Utility.Pricelist.Export());
}